using Gateway.Common.Enums;
using Gateway.Domain.Entities;
using Gateway.IApplication.SystemSetting;
using Gateway.IApplication.SystemSetting.Input;
using Gateway.IRepositories;
using Microsoft.EntityFrameworkCore;

namespace Gateway.Application;
/// <summary>
/// 
/// </summary>
public class SystemSettingApp : ISystemSettingApp
{
    private readonly IAppRepository<SystemSetting> _settingRepository;
    private readonly IAppRepository<SystemExtensionSetting> _extensionRepository;
    private readonly IUnitOfWork _unitOfWork;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="settingRepository"></param>
    /// <param name="extensionRepository"></param>
    /// <param name="unitOfWork"></param>
    public SystemSettingApp(IAppRepository<SystemSetting> settingRepository,
        IAppRepository<SystemExtensionSetting> extensionRepository, IUnitOfWork unitOfWork)
    {
        _settingRepository = settingRepository;
        _extensionRepository = extensionRepository;
        _unitOfWork = unitOfWork;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<bool> Set(CreateSystemSettingInput input)
    {
        //查询 系统设置中是否有数据
        var entity = await _settingRepository.Entities.Include(x => x.Sms)
            .FirstOrDefaultAsync(p => p.Deleted == BooleanStatusEnum.No);
        if (entity != null)
        {
            //执行更新
            entity.Update(systemName: input.SystemName, logoUrl: input.LogoUrl, loginType: input.LoginType,
                passwordRegex: input.PasswordRegex, copyright: input.Copyright);
            if (input.Sms != null)
            {
                entity.SetSms(new SystemSms()
                {
                    AppKey = input.Sms.AppKey,
                    SecretKey = input.Sms.SecretKey,
                    Status = BooleanStatusEnum.FromValue(input.Sms.Status)
                });
            }

            await _settingRepository.UpdateAsync(entity);
            await SetExtension(input.Extensions);
        }
        else
        {
            //执行插入
            entity = new SystemSetting(systemName: input.SystemName, logoUrl: input.LogoUrl, loginType: input.LoginType,
                passwordRegex: input.PasswordRegex, copyright: input.Copyright);
            if (input.Sms != null)
            {
                entity.SetSms(new SystemSms()
                {
                    AppKey = input.Sms.AppKey,
                    SecretKey = input.Sms.SecretKey,
                    Status = BooleanStatusEnum.FromValue(input.Sms.Status)
                });
            }

            await _settingRepository.AddAsync(entity);
            await SetExtension(input.Extensions);
        }

        await _unitOfWork.CommitAsync();
        return true;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="inputs"></param>
    /// <returns></returns>
    private async Task SetExtension(List<SystemExtensionSettingInput> inputs)
    {
        var all = await _extensionRepository.GetListAsync(p => p.Deleted == BooleanStatusEnum.No);
        if (all.Count > 0)
        {
            foreach (var item in all)
            {
                item.SetDeleted();
                await _extensionRepository.UpdateAsync(item);
            }
        }

        if (inputs.Count > 0)
        {
            foreach (var item in inputs)
            {
                var entity = new SystemExtensionSetting(key: item.Key, value: item.Value, category: item.Category,
                    title: item.Title);
                await _extensionRepository.AddAsync(entity);
            }
        }
    }
}